<?php
require_once './system/common.inc.php';

if(!$uid){
	header('Location: member.php?action=login');
	exit();
}
if($_GET['action']){
	switch($_GET['action']){
		case 'edit':
			$id = $_GET['id'];
			if(!$id) exit('Access Denied');
			$query = DB::query("SELECT * FROM songs WHERE id='{$id}'");
			while($result = DB::fetch($query)) $songdata = $result;
			if($songdata['uid']!=$uid) showmessage('用户匹配错误');
			include template('edit');
			break;
		case 'doedit':
			$id = $_POST['id'];
			$title = $_POST['title'];
			$artist = $_POST['artist'];
			$album = $_POST['album'];
			if(!$id || !$title || !$artist || !$album) exit('Access Denied');
			$query = DB::query("SELECT * FROM songs WHERE id='{$id}'");
			while($result = DB::fetch($query)) $songdata = $result;
			if($songdata['uid']!=$uid) showmessage('用户匹配错误');
			if($songdata['status']=='1') $status = '2';
			elseif($songdata['status']=='2') $status = '2';
			else $status = '3';
			if($songdata['type']=='0') $type = $_POST['type'];
			else $type = $songdata['type'];
			DB::update('songs',array(
				'title' => $title,
				'artist' => $artist,
				'album' => $album,
				'status' => $status,
				'type' => $type,
			),array('id'=>$id));
			showmessage('资料更新成功','my.php');
			break;
		case 'delete':
			$id = $_GET['id'];
			if(!$id) exit('Access Denied');
			$query = DB::query("SELECT * FROM songs WHERE id='{$id}'");
			while($result = DB::fetch($query)) $songdata = $result;
			if($songdata['uid']!=$uid) showmessage('用户匹配错误');
			DB::query("DELETE FROM songs WHERE id='{$id}'");
			showmessage('删除歌曲成功','my.php');
			break;
	}
	exit;
}

$query = DB::query("SELECT count(*) FROM songs WHERE uid='{$uid}'");
while($result = DB::fetch($query)) $counts = $result['count(*)'];
$Page = new Page($counts,10);
$Page->url = 'my.php?page=';
$query = DB::query("SELECT * FROM songs WHERE uid='{$uid}' ORDER BY `id` DESC LIMIT {$Page->firstRow},{$Page->listRows}");
$songs = array();
while($result = DB::fetch($query)) $songs[] = $result;

include template('my');